from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pymysql

import interface.main0 as main0
import interface.login as login
import interface.admin.admin_main0_Window as admin_main0_Window
import database.admin.setting as setting
import interface.student.stu_query_history_course_window as stu_query_history_course_window
import interface.student.stu_main0_Window as stu_main0_Window
import interface.teacher.tea_main0_Window as tea_main0_Window
class Login(QMainWindow,login.Ui_Dialog):###点击用户身份弹出的登陆小弹窗
    def __init__(self,people):
        QMainWindow.__init__(self)
        login.Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.people=people
        if(self.people=="学生"):
            self.setWindowTitle("学生登陆窗口")
        elif(self.people=="教师"):
            self.setWindowTitle("教师登陆窗口")
        else :
            self.setWindowTitle("教务处管理员登陆窗口")
        self.cancel.clicked.connect(self.hide)
        self.id.setPlaceholderText("请输入学号/工号")
        self.passwd.setPlaceholderText("请输入密码")
        self.passwd.setEchoMode(QLineEdit.Password)
        self.id_=self.id.text()
        self.passwd_=self.passwd.text()
        self.ok.clicked.connect(self.ok_)
    def closewin(self):
        self.close()
    def ok_(self):
        self.id_ = self.id.text()
        self.passwd_ = self.passwd.text()
        #print(self.id_, self.passwd_)
class Main0_Window(QMainWindow,main0.Ui_Form):####主窗口，即打开软件看到的界面
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowTitle("Xmslu教务管理系统")
        s0_ = QPixmap("s0.png")
        self.s0.setPixmap(s0_)
        self.s0.setScaledContents(True)
        s00_ = QPixmap("s00.png")
        self.s00.setPixmap(s00_)
        self.s00.setScaledContents(True)
        s1_ = QPixmap("s1.png")
        self.s1.setPixmap(s1_)
        self.s1.setScaledContents(True)
        s2_ = QPixmap("s2.png")
        self.s2.setPixmap(s2_)
        self.s2.setScaledContents(True)
        s3_ = QPixmap("s3.png")
        self.s3.setPixmap(s3_)
        self.s3.setScaledContents(True)
        #self.label.adjustSize()
        self.stu.clicked.connect(self.stulogin)
        self.tea.clicked.connect(self.tealogin)
        self.jwc.clicked.connect(self.jwclogin)
        self.Cancel.clicked.connect(QCoreApplication.instance().quit)
        self.config=setting.config
        self.cursor=None
        p=QPalette()
        p_=QPixmap("s000.jpg")
        p_=p_.scaled(self.width(),self.height())
        p.setBrush(QPalette.Background,QBrush(p_))
        self.setPalette(p)
        print(self.config)

        timer = QTimer(self)
        timer.timeout.connect(self.showtime)
        timer.start()
    def showtime(self):
        datetime = QDateTime.currentDateTime()
        text = datetime.toString(Qt.DefaultLocaleLongDate)
        #print((text))
        self.time_label.setText("   " + text)
    def verify_login_stu(self):
        self.connect = pymysql.connect(**self.config)
        self.identity="学生"
        #print(self.id)
        #print(self.passwd)
        self.id = self.stu_login.id_
        self.passwd = self.stu_login.passwd_
        # self.cursor=self.connect.cursor(pymysql.cursors.DictCursor)
        print("Stu_Log_In Sucess")
        self.cursor = self.connect.cursor()
        print(self.id)
        sql_=("select id,passwd,iden from id_passwd where id = '%s' ")%(str(self.id))
        #sql_ = "select id,passwd,iden from id_passwd  "
        self.cursor.execute(sql_)
        re=self.cursor.fetchall()
        self.cursor.close()
        self.connect.close()
        if (len(re) == 0):
            print("Having No Such People")
            QMessageBox.critical(self, "错误", "该用户不存在！")
        else:
            real_passwd=re[0][1]
            real_identity=re[0][2]
            if(real_identity!=self.identity):
                print("Wrong")
                QMessageBox.critical(self,"错误","请输入学生用户账号！")
            else:
                if(real_passwd!=self.passwd):
                    print("Wrong--PassWd")
                    QMessageBox.critical(self, "错误", "密码错误！")
                else:
                    print("Hello")
                    #self.stu_main=stu_query_history_course_window.Stu_QHC(self.id)
                    self.stu_main=stu_main0_Window.stu_main0_window(self.id)
                    self.stu_main.show()
                    self.stu_login.close()
                    self.close()

    def verify_login_tea(self):
        self.connect = pymysql.connect(**self.config)
        self.identity = "教师"
        # print(self.id)
        # print(self.passwd)
        self.id = self.tea_login.id_
        self.passwd = self.tea_login.passwd_
        # self.cursor=self.connect.cursor(pymysql.cursors.DictCursor)
        print("Tea_Log_In Sucess")
        self.cursor = self.connect.cursor()
        print(self.id)
        sql_ = ("select id,passwd,iden from id_passwd where id = '%s' ") % (str(self.id))
        # sql_ = "select id,passwd,iden from id_passwd  "
        self.cursor.execute(sql_)
        re = self.cursor.fetchall()
        self.cursor.close()
        self.connect.close()
        if (len(re) == 0):
            print("Having No Such People,Wrong!")
            QMessageBox.critical(self, "错误", "该用户不存在！")
        else:
            real_passwd = re[0][1]
            real_identity = re[0][2]
            if (real_identity != self.identity):
                print("Wrong")
                QMessageBox.critical(self, "错误", "请输入教师用户账号！")
            else:
                if (real_passwd != self.passwd):
                    print("Wrong--PassWd")
                    QMessageBox.critical(self, "错误", "密码错误！")
                else:
                    print("Hello")
                    self.tea_main = tea_main0_Window.tea_main0_Window(self.id)
                    self.tea_main.show()
                    self.tea_login.close()
                    self.close()

    def verify_login_jwc(self):
        # print(self.id)
        # print(self.passwd)
        self.id = self.jwc_login.id_
        self.passwd = self.jwc_login.passwd_
        #print(self.id,self.passwd)
        #print(setting.Jwc_id,setting.Jwc_passwd)
        if(self.id!=setting.Jwc_id or self.passwd!=setting.Jwc_passwd):
            print("Wrong")
            QMessageBox.critical(self, "错误", "账号密码错误！")
        else:
            self.jwc_main=admin_main0_Window.admin_main0_window()
            self.jwc_main.show()
            self.jwc_login.close()
            self.close()


    def stulogin(self):
        self.stu_login=Login("学生")
        #print("Succ")
        self.stu_login.ok.clicked.connect(self.verify_login_stu)
        self.stu_login.show()
    def tealogin(self):
        self.tea_login = Login("教师")
        # print("Succ")
        self.tea_login.ok.clicked.connect(self.verify_login_tea)
        self.tea_login.show()
    def jwclogin(self):
        self.jwc_login = Login("教务处管理员")
        self.jwc_login.ok.clicked.connect(self.verify_login_jwc)
        # print("Succ")
        self.jwc_login.show()
if __name__=='__main__':
    app=QApplication(sys.argv)
    md=Main0_Window()
    md.show()
    sys.exit(app.exec_())